package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     */
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        //1. 根据员工账号查询 select * from employee where username=?
        Employee emp = employeeService.query()
                .eq("username", employee.getUsername())
                .one();
        if (emp == null) {//说明账号不存在
            return R.error("账号不存在");
        }
        //2. 对比前端提交的密码和数据库查询出来的密码
        String password1 = employee.getPassword();
        String password2 = emp.getPassword();
        boolean flag = StringUtils.equals(DigestUtils.md5Hex(password1), password2);
        if (!flag) {
            return R.error("密码错误");
        }
        //3. 判断员工状态
        if (emp.getStatus() == 0) {
            return R.error("账号已被禁用");
        }
        //4. 员工登录成功，将员工的id存入session
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }


    /**
     * 员工退出
     */
    @PostMapping("logout")
    public R<String> logout(HttpSession session) {
        //1. 销毁session
        session.invalidate();
        return R.success("退出成功");
    }


    /**
     * 添加员工
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpSession session) {
        //补全信息
        employee.setPassword(DigestUtils.md5Hex("123456"));//默认123456
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

        Long empId = (Long) session.getAttribute("employee");
        /*employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);
        return R.success("添加成功");
    }


    /**
     * 分页条件查询
     */
    @GetMapping("page")
    public R<Page<Employee>> page(int page, int pageSize, String name) {
        //1. 创建分页对象
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //2. 分页查询
        employeeService.query()
                .like(StringUtils.isNotBlank(name), "name", name)
                .orderByDesc("update_time")
                .page(pageInfo);
        //3. 返回数据
        return R.success(pageInfo);
    }


    /**
     * 启用、禁用员工状态
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpSession session) {
        // update employee set status=? where id=?
        employeeService.updateById(employee);
        return R.success("修改成功");
    }


    /**
     * 根据id查询
     */
    @GetMapping("{id}")
    public R<Employee> findById(@PathVariable Long id) {
        return R.success(employeeService.getById(id));
    }
}
